<?php

class siteFunctions
{
	var $loginTag;
	var $extra;
	var $mode;
	var $layout;
	var $getPageName;
	var $getpgName_replace;
	var $pgName;
	var $BtnName;
	var $arr_message;
	var $arr_key;
	var $arr_val;
	var $getMsgString;
	var $email_array;
	var $local_array;
	var $i;
	var $domain_array;
	var $hdr;
	var $ENCODEDstring;
	var $ERRORmessage;
	var $RETURNvalue;
	var $USERsessionID;
	var $PAGEtiTLe;
	var $rand_index;
	var $random_generator;
	
	/* ============  this is for Constructor strats here =========================*/
	
	function siteFunctions()
	{
		$this->loginTag='';
		$this->extra='';
		$this->mode='';
		$this->layout='';
		$this->getPageName='';
		$this->getpgName_replace='';
		$this->pgName='';
		$this->BtnName='';
		$this->arr_message='';
		$this->arr_key='';
		$this->arr_val='';
		$this->getMsgString='';
		$this->email_array='';
		$this->local_array='';
		$this->i='';
		$this->domain_array='';
		$this->hdr='';
		$this->ENCODEDstring='';
		$this->ERRORmessage='';
		$this->RETURNvalue='';
		$this->USERsessionID='';
		$this->rand_index='';
		$this->random_generator='';
	}
	
	
	/* ============  this is for Constructor ends here =========================*/
	
	/* ============  this is for login tag at the top of the box =========================*/
	function isLoginTag()
	{
		if(IsSetSession('USERID'))
			$this->loginTag = 'Hi, <strong>'.$_SESSION['USERNAME'].'</strong>&nbsp; I &nbsp;<a href="'.$GLOBALS['SiteUrl'].'logout.php">Logout</a>';
		else	
			$this->loginTag = '<a href="'.$GLOBALS['SiteUrl'].'sign_up.php">Register</a>&nbsp; I &nbsp;<a href="'.$GLOBALS['SiteUrl'].'login.php">Login</a>';
			
		echo $this->loginTag;	
	}
	/* ============  this is for changing HEAD of the web page for logged in user & logged out user =========================*/
	function isLoginHead()
	{
		if(isset($_SESSION['USERID']) && $_SESSION['USERID']!='')
		include('head.php');
		else	
		include('head1.php');
	
	}
	/* ============  this is for changing FOOT of the web page for logged in user & logged out user =========================*/
	function isLoginFoot()
	{
		if(isset($_SESSION['USERID']) && $_SESSION['USERID']!='')
		include('foot.php');
		else	
		include('foot1.php');
	
	}
	/* ============  this is for checking authentication for a user =========================*/
	
	function checkLogin($userID, $extra = false)
	{
			if(!isset($userID))
			{
				if($extra!='')
				{
						$_SESSION['PAGENAME'] = $extra;
					header( 'Location: ' . $GLOBALS['AbsolutePath'] . "login.php" );
				}
				else
				{	
					header( 'Location: ' . $GLOBALS['AbsolutePath'] . "login.php" );
				}
					exit();
			}
			else
				return true;
	}
		/*========= this function calls the application or action page executing the php scripts with extension .php =================*/
		function inputSubmit($var)
		{
			$this->getPageName = str_replace('btn','',$var);
			require_once 'app/app'.$this->getPageName.'.php';
		}
		
		/*========= this function calls the application or action page executing the php scripts with extension .php For The BAckend =================*/
		function inputSubmitScript($var)
		{
			$this->getPageName = str_replace('btn','',$var);
			require_once 'appScript/appScript'.$this->getPageName.'.php';
		}
		
		/*========= this function calls the HTML Page For Displaying The Result =================*/
		function inputHTML($var)
		{
			#echo $var.'<br>';
			
			$this->getPageName = str_replace('action','',$var);
			//include $GLOBALS['PhysicalPath'].'structure/HTML'.$getPageName;
			#echo 'structure/HTML'.$getPageName.'.php';
			
			//include_once 'structure/HTML'.$getPageName.'.php';
			
			return ('structure/HTML'.$this->getPageName.'.php');
		}
		
		
		/*============ this function returns the submit type name present in the form from where its being called ==============*/
		function getButtonName($pgName)
		{
			$this->getpgName_replace = str_replace('app','',$pgName);
			$this->BtnName = 'btn'.$this->getpgName_replace;
			return $this->BtnName;
		}
		
		
		/*============ this function matches the message string according to the $_GET value and returns the message string ==============*/
		function getMessageString($message)
		{
			$this->arr_message=$GLOBALS['msgArray'];
			
			foreach( $this->arr_message as $this->arr_key => $this->arr_val )
			{
					if( $this->arr_key == $message )
						return $this->arr_val;
						
			}			
		}
		
		/*
		============ this function  returns the message string in a nice html box==============
		u need to include the css for this function which is written in global.css
		*/
	function SERVER_MESSAGE($message, $mode = '')
	{
		$this->getMsgString = getMessageString($message);
		if($mode == '')
		{
			return '<div class="e_main">
				<div class="e_top"><table width="100%" border="0" cellspacing="0" cellpadding="0">
					<tr>
						<td width="1%" height="27">&nbsp;</td>
						<td width="66%" align="left"><strong>Error Reporting</strong></td>
						<td width="33%">&nbsp;</td>
					</tr>
				</table></div>
				<div align="center"><table width="100%" border="0" cellspacing="0" cellpadding="0">
					<tr>
						<td height="27" align="left"><strong>&nbsp;&nbsp;'.$this->getMsgString.'</strong></td>
					</tr>
				</table></div>
			</div>';
		}
		else
		{
			return '<div class="e_main_g">
				<div class="e_top_g"><table width="100%" border="0" cellspacing="0" cellpadding="0">
					<tr>
						<td width="1%" height="27">&nbsp;</td>
						<td width="66%">&nbsp;</td>
						<td width="33%">&nbsp;</td>
					</tr>
				</table></div>
				<div align="center"><table width="100%" border="0" cellspacing="0" cellpadding="0">
					<tr>
						<td height="27" align="left"><strong>'.$this->getMsgString.'</strong></td>
					</tr>
				</table></div>
			</div>';
		}
	}
	
		/*	============ check EmailAddress ===============	*/
		function CheckEmailAddress( $email )
		{
			if (!ereg("^[^@]{1,64}@[^@]{1,255}$", $email))
			{
				return false;
			}
			$this->email_array = explode("@", $email);
			$this->local_array = explode(".", $this->email_array[0]);
			for ($this->i = 0; $this->i < sizeof($this->local_array); $this->i++)
			{
				if (!ereg("^(([A-Za-z0-9!#$%&'*+/=?^_`{|}~-][A-Za-z0-9!#$%&'*+/=?^_`{|}~\.-]{0,63})|(\"[^(\\|\")]{0,62}\"))$", $this->local_array[$this->i]))
				{
					return false;
				}
			}
			if (!ereg("^\[?[0-9\.]+\]?$", $this->email_array[1]))
			{
				$this->domain_array = explode(".", $this->email_array[1]);
				if (sizeof($this->domain_array) < 2)
				{
					return false;
				}
				for ($this->i = 0; $this->i < sizeof($this->domain_array); $this->i++)
				{
					if (!ereg("^(([A-Za-z0-9][A-Za-z0-9-]{0,61}[A-Za-z0-9])|([A-Za-z0-9]+))$", $this->domain_array[$this->i]))
					{
						return false;
					}
				}
			}
			return true;
		}
	
		function getHeaderForThanxError($var)
		{
			if(isset($var))
			{
				if($var=='registersuccess' || $var=='registerunsuccess')
					$this->hdr = 'Register';
					
					
					return $this->hdr;
			}
			else
				return 'Coding Error';
		}	
		
		function GetEncoded($ARGUMENT)
		{
			$this->ENCODEDstring=md5($ARGUMENT);
			return ($this->ENCODEDstring);
		}	
		
		function RedirectTo($RedirectionUrl)
		{
			//header("Location: ".$RedirectionUrl);
			echo '<script>window.location="'.$RedirectionUrl.'"</script>';
		}
		
		function RedirectPageTo($RedirectionUrl)
		{
			
			$this->getPageName = str_replace('Page','',str_replace('page','',$RedirectionUrl));
			$this->RETURNvalue=$this->getPageName.'.php';
			echo '<script>window.location="'.$this->RETURNvalue.'"</script>';
		}
		function GETERRORmessage($ERRORmessage)
		{
			return $this->ERRORmessage=$ERRORmessage;
		}
		function GETVALUE($FIELDname)
		{
			return $this->RETURNvalue=$_GET[$FIELDname];
		}
		function POSTVALUE($FIELDname)
		{
			return $this->RETURNvalue=$_POST[$FIELDname];
		}
		function REQUESTVALUE($FIELDname)
		{
			return $this->RETURNvalue=$_REQUEST[$FIELDname];
		}
		function SESSIONVALUE($FIELDname)
		{
			//echo $FIELDname;
			//echo '<br><br>'.$_SESSION[$FIELDname];
			return $this->RETURNvalue=$_SESSION[$FIELDname];
		}
		function COOKIEVALUE($FIELDname)
		{
			return $this->RETURNvalue=$_COOKIE[$FIELDname];
		}
		
		function SETCOOKIE($arg1,$arg2,$arg3,$arg4='',$arg5='')
		{
			//echo 'SETCOOKIE=============='.$arg1.','.$arg2.','.$arg3.','.$arg4.'<br><br>';
			return $this->RETURNvalue=setcookie($arg1,$arg2,$arg3,$arg4);
		}
		
		
		function CREATEUSERID($SESSIONID)
		{
			
			return $this->USERsessionID=$SESSIONID.time();
		}
		function GETPAgename($PAGETITLE)
		{
			$baseNAme=explode(".php",basename($PAGETITLE));
			return $this->PAGEtiTLe=$baseNAme;
		}
		function PRINTSTRING($ARGUMENTSTRING)
		{
			//$this->RETURNvaLUE=str_replace("PrintArray(","print_r(",str_replace("Print(","echo",str_replace(");",'',str_replace(")",")",$ARGUMENTSTRING))));
			$this->RETURNvaLUE= $ARGUMENTSTRING;			
			echo $this->RETURNvaLUE;
			
		}
		
		function PRINTF_STRING($ARGUMENTSTRING,$FORMAT)
		{
			$this->RETURNvaLUE= $ARGUMENTSTRING;			
			printf($FORMAT,$this->RETURNvaLUE);
			
		}
		function UPPERCASE($ARGUMENTSTRING,$CHANGETYPE)
		{
			if($CHANGETYPE == "UPPERCHAR")
			{
				return ucfirst($ARGUMENTSTRING);
			}
			else
			{
				return ucwords($ARGUMENTSTRING);
			}			
		}
		
				
		function PRINTARRAY($ARGUMENTSTRING)
		{
			//echo $PAGETITLE;
			//$this->RETURNvaLUE=str_replace("PrintArray(","print_r(",str_replace(")",")",$ARGUMENTSTRING));
			//return $this->RETURNvaLUE;
			print_r($ARGUMENTSTRING);
			
		}
		
		function ARRAycounTER($ARGUMENTSTRING)
		{
			$this->RETURNvaLUE=count($ARGUMENTSTRING);
			return $this->RETURNvaLUE;
		}
				
		function REPlaceLINk($TEXT)
		{
			$patterns=array('/<(a.*) href=\"(.*?)\">/i','/<\/a>/i');
			$replacements=array('','');
			$this->RETURNvaLUE=preg_replace($patterns,$replacements,$TEXT);
			return $this->RETURNvaLUE;
		}
		
		function PARSINGagainstTHEADER($TEXT)
		{
			$regexp = "<th\s[^>]*class=(\"??)([^\" >]*?)\\1[^>]*>(.*)<\/th>"; 
			if(preg_match_all("/$regexp/siU", $TEXT, $matches)) 
			{ 
				# $matches[2] = array of link addresses 
				# $matches[3] = array of link text - including HTML code 
				//print_r($matches[3]);
				return $matches[3];
			}		
		}
		
		function PARSINGagainstTD($TEXT)
		{
			$regexp = "<td\s[^>]*class=(\"??)([^\" >]*?)\\1[^>]*>(.*)<\/td>"; 
			if(preg_match_all("/$regexp/siU", $TEXT, $matches)) 
			{ 
				# $matches[2] = array of link addresses 
				# $matches[3] = array of link text - including HTML code 
				//print_r($matches[3]);
				return $matches[3];
			}		
		}
		
		function GETStrifTIme($argument,$separation='')
		{
			if($separation != '')
			{
				$this->RETURNvalue=strftime('%Y'.$separation.'%m'.$separation.'%d', $argument);
			}
			else
			{
				$this->RETURNvalue=strftime('%Y-%m-%d', $argument);
			}
			return $this->RETURNvalue;
		}
		
		
		
		
		function GETPAConpressedVALUE($FIELDname)
		{
			//echo $FIELDname;die();
			return $this->RETURNvalue=trim($FIELDname);
		}

		function GETEXPLODEVALUE($FIELDname,$explodeby,$FIELDNUMBER='')
		{
			$this->RETURNvalue=explode($explodeby,$FIELDname);
			if($FIELDNUMBER == '')
				return $this->RETURNvalue;
			else
				return $this->RETURNvalue[$FIELDNUMBER];
		}

		function STRINGcompaiRe($GETSTRing1,$GETSTRing2)
		{
			return $this->RETURNvalue=stristr($GETSTRing1,$GETSTRing2);
		}
		function REPLACETEXTBYyourtext($REPLACETEXT,$YOURTEXT,$MAINTEXT)
		{
			return $this->RETURNvalue=str_replace($REPLACETEXT,$YOURTEXT,$MAINTEXT);
		}
		function MAKEPROPERtextFORMAT($GIVENTEXT)
		{
			return $this->RETURNvalue=nl2br($GIVENTEXT);
		}
		
		function PAGECOMEfrom($REQUEST)
		{
			if($REQUEST == 'RefferPage')
			{
				$this->getPageName = str_replace($REQUEST ,"HTTP_REFERER",$REQUEST);
			}
			elseif($REQUEST == 'RequestPage')
			{
				$this->getPageName = str_replace($REQUEST ,"REQUEST_URI",$REQUEST);
			}
			elseif($REQUEST == 'SelfPage')
			{
				$this->getPageName = str_replace($REQUEST ,"PHP_SELF",$REQUEST);
			}
			//echo $this->getPageName;
			
			//$this->getPageName = str_replace("RefferPage","HTTP_REFERER",str_replace("RequestPage","REQUEST_URI",str_replace("SelfPage","PHP_SELF")));
			return $this->RETURNvalue=basename($_SERVER[$this->getPageName]);
		}
		

		
		
		
		function SENDmaIL($RECEIPENT,$SUBJECT,$MESSAGE,$FROM,$REPLYTO='')
		{
						
			$HEADERs='MIME-Version: 1.0\r\n';
			$HEADERs.='Content-Type: text/html; charset=iso-8859-1\r\n'; 
			$HEADERs.='From: '.$FROM.' <'.$REPLYTO.'>'."\r\n";
			return $this->RETURNvalue=mail($RECEIPENT, $SUBJECT, $MESSAGE, $HEADERs);
		}
		
		function random_generator($digits)
		{
			srand ((double) microtime() * 10000000);
			//Array of alphabets
			$input = array ("A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z");
			
			for($i=1;$i<$digits+1;$i++) // Loop the number of times of required digits
			{ 
				if(rand(1,2) == 1) // to decide the digit should be numeric or alphabet
				{
					// Add one random alphabet 
					$this->rand_index = array_rand($input);
					$this->random_generator .=$input[$this->rand_index]; // One char is added
				
				}
				else
				{
					// Add one numeric digit between 1 and 10
					$this->random_generator .=rand(1,10); // one number is added
				} // end of if else
			} // end of for loop 
			
			return $this->random_generator;
		} // end of function

		
}
?>
